package cc.rengu.oltp.service.realize.impl;

import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.bean.RedisConfig;
import cc.rengu.oltp.service.realize.RedisService;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.*;

import java.util.*;
import java.util.stream.Collectors;

/**
 * Redis服务实现类
 * Created by wangc on 2020/12/14.
 */
public class RedisServiceImpl implements RedisService {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private static Object redisClient;     /* Redis客户端 */
    private static boolean clusterFlag = false;   /* Redis集群部署标识 */
    private static String clusterMode = "cluster";  /* Redis集群默认工作模式 */
    private static final String REDIS_WORK_MODE_CLUSER = "cluster";    /* Redis集群工作模式（服务端集群） */
    private static final String REDIS_WORK_MODE_SENTINEL = "sentinel"; /* Resis集群哨兵工作模式 */
    private static final String REDIS_WORK_MODE_SHARDED = "sharded";  /* Resis集群分片工作模式（客户端集群） */

    @Override
    public Object redisClientInit(RedisConfig redisConfig) {
        if (redisConfig.isClusterFlag()) {
            if (REDIS_WORK_MODE_CLUSER.equals(redisConfig.getClusterMode())) {
                redisClient = initJedisCluster(redisConfig);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(redisConfig.getClusterMode())) {
                redisClient = initJesitSentinelPool(redisConfig);
            } else if (REDIS_WORK_MODE_SHARDED.equals(redisConfig.getClusterMode())) {
                redisClient = initShardedJedisPoll(redisConfig);
            } else {
                rglog.error("不支持的集群工作模式:{}", redisConfig.getClusterMode());
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            redisClient = initJedisPool(redisConfig);
        }
        clusterFlag = redisConfig.isClusterFlag();
        clusterMode = redisConfig.getClusterMode();
        return redisClient;
    }

    @Override
    public boolean set(String key, String value) {
        String returnCode = "FAIL";
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                returnCode = jedisCluster.set(key, value);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    returnCode = jedis.set(key, value);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    returnCode = shardedJedis.set(key, value);
                }
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                returnCode = jedis.set(key, value);
            }
        }
        return "OK".equals(returnCode);
    }

    @Override
    public boolean set(String key, String value, int activeTime) {
        String returnCode = "FAIL";
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                returnCode = jedisCluster.set(key, value);
                if ("OK".equals(returnCode)) {
                    jedisCluster.pexpire(key, activeTime * 1000L);
                }
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    returnCode = jedis.set(key, value);
                    if ("OK".equals(returnCode)) {
                        jedis.pexpire(key, activeTime * 1000L);
                    }
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    returnCode = shardedJedis.set(key, value);
                    if ("OK".equals(returnCode)) {
                        shardedJedis.pexpire(key, activeTime * 1000L);
                    }
                }
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                returnCode = jedis.set(key, value);
                if ("OK".equals(returnCode)) {
                    jedis.pexpire(key, activeTime * 1000L);
                }
            }
        }
        return "OK".equals(returnCode);
    }

    @Override
    public String get(String key) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.get(key);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.get(key);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.get(key);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.get(key);
            }
        }
    }

    @Override
    public String getAndSet(String key, String value) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.getSet(key, value);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.getSet(key, value);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.getSet(key, value);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.getSet(key, value);
            }
        }
    }

    @Override
    public long incr(String key) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.incr(key);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.incr(key);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.incr(key);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.incr(key);
            }
        }
    }

    @Override
    public long decr(String key) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.decr(key);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.decr(key);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.decr(key);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.decr(key);
            }
        }
    }

    @Override
    public boolean exists(String key) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.exists(key);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.exists(key);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.exists(key);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.exists(key);
            }
        }
    }

    @Override
    public long hashSet(String key, String field, String value) {
        long keyValue;
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                keyValue = jedisCluster.hset(key, field, value);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.hset(key, field, value);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.hset(key, field, value);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                keyValue = jedis.hset(key, field, value);
            }
        }
        return keyValue;
    }

    @Override
    public long hashSet(String key, String field, String value, int activeTime) {
        long keyValue;
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                keyValue = jedisCluster.hset(key, field, value);
                if (keyValue > 0) {
                    jedisCluster.pexpire(key, activeTime * 1000L);
                }
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    keyValue = jedis.hset(key, field, value);
                    if (keyValue > 0) {
                        jedis.pexpire(key, activeTime * 1000L);
                    }
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    keyValue = shardedJedis.hset(key, field, value);
                    if (keyValue > 0) {
                        shardedJedis.pexpire(key, activeTime * 1000L);
                    }
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                keyValue = jedis.hset(key, field, value);
                if (keyValue > 0) {
                    jedis.pexpire(key, activeTime * 1000L);
                }
            }
        }
        return keyValue;
    }

    @Override
    public String hashGet(String key, String field) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.hget(key, field);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.hget(key, field);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.hget(key, field);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.hget(key, field);
            }
        }
    }

    @Override
    public boolean hashMultSet(String key, Map<String, String> filedValue) {
        if (null == filedValue || filedValue.isEmpty()) {
            return false;
        } else {
            String returnCode;
            if (clusterFlag) {
                if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                    JedisCluster jedisCluster = (JedisCluster) redisClient;
                    returnCode = jedisCluster.hmset(key, filedValue);
                } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                    try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                        returnCode = jedis.hmset(key, filedValue);
                    }
                } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                    try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                        returnCode = shardedJedis.hmset(key, filedValue);
                    }
                } else {
                    rglog.error("不支持的集群工作模式:{}", clusterMode);
                    throw new BizException("不支持的集群工作模式");
                }
            } else {
                try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                    returnCode = jedis.hmset(key, filedValue);
                }
            }
            return "OK".equals(returnCode);
        }
    }

    @Override
    public List<String> hashMultGet(String key, String... fields) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.hmget(key, fields);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.hmget(key, fields);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.hmget(key, fields);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.hmget(key, fields);
            }
        }
    }

    @Override
    public long getHashLen(String key) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.hlen(key);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.hlen(key);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.hlen(key);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.hlen(key);
            }
        }
    }

    @Override
    public Map<String, String> hashGetAll(String key) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.hgetAll(key);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.hgetAll(key);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.hgetAll(key);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.hgetAll(key);
            }
        }
    }

    @Override
    public Set<String> hashKeys(String key) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.hkeys(key);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.hkeys(key);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.hkeys(key);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.hkeys(key);
            }
        }
    }

    @Override
    public boolean hashExists(String key, String field) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.hexists(key, field);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.hexists(key, field);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.hexists(key, field);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.hexists(key, field);
            }
        }
    }

    @Override
    public long hashDelete(String key, String... fields) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.hdel(key, fields);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.hdel(key, fields);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.hdel(key, fields);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = (Jedis) redisClient) {
                return jedis.hdel(key, fields);
            }

        }
    }

    @Override
    public long hashIncrease(String key, String field, long increment) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.hincrBy(key, field, increment);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.hincrBy(key, field, increment);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.hincrBy(key, field, increment);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.hincrBy(key, field, increment);
            }
        }
    }

    @Override
    public long getListLen(String key) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.llen(key);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.llen(key);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.llen(key);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.llen(key);
            }
        }
    }

    @Override
    public List<String> listRange(String key, long start, long end) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.lrange(key, start, end);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.lrange(key, start, end);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.lrange(key, start, end);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.lrange(key, start, end);
            }
        }
    }

    @Override
    public boolean listTrim(String key, long start, long end) {
        String returnCode;
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                returnCode = jedisCluster.ltrim(key, start, end);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    returnCode = jedis.ltrim(key, start, end);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    returnCode = shardedJedis.ltrim(key, start, end);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                returnCode = jedis.ltrim(key, start, end);
            }
        }
        return "OK".equals(returnCode);
    }

    @Override
    public boolean listSet(String key, long index, String value) {
        String returnCode;
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                returnCode = jedisCluster.lset(key, index, value);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    returnCode = jedis.lset(key, index, value);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    returnCode = shardedJedis.lset(key, index, value);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                returnCode = jedis.lset(key, index, value);
            }
        }
        return "OK".equals(returnCode);
    }

    @Override
    public String listGet(String key, long index) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.lindex(key, index);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.lindex(key, index);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.lindex(key, index);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.lindex(key, index);
            }
        }
    }

    @Override
    public long listRem(String key, long count, String value) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.lrem(key, count, value);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.lrem(key, count, value);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.lrem(key, count, value);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.lrem(key, count, value);
            }
        }
    }

    @Override
    public long listLeftPush(String key, String... values) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.lpush(key, values);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.lpush(key, values);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.lpush(key, values);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.lpush(key, values);
            }
        }
    }

    @Override
    public long listLeftPushX(String key, String... values) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.lpushx(key, values);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.lpushx(key, values);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.lpushx(key, values);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.lpushx(key, values);
            }
        }
    }

    @Override
    public String listLeftPop(String key) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.lpop(key);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.lpop(key);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.lpop(key);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.lpop(key);
            }
        }
    }

    @Override
    public long listRightPush(String key, String... values) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.rpush(key, values);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.rpush(key, values);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.rpush(key, values);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.rpush(key, values);
            }
        }
    }

    @Override
    public long listRightPushX(String key, String... values) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.rpushx(key, values);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.rpushx(key, values);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.rpushx(key, values);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.rpushx(key, values);
            }
        }
    }

    @Override
    public String listRightPop(String key) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.rpop(key);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.rpop(key);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.rpop(key);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.rpop(key);
            }
        }
    }

    @Override
    public long delete(String... keys) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.del(keys);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.del(keys);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    long delNum = 0;
                    for (String key : keys) {
                        delNum += shardedJedis.del(key);
                    }
                    return delNum;
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.del(keys);
            }
        }
    }

    @Override
    public long setKeyExpire(String key, int activeTime) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.pexpire(key, activeTime * 1000L);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.pexpire(key, activeTime * 1000L);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.pexpire(key, activeTime * 1000L);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.pexpire(key, activeTime * 1000L);
            }
        }
    }

    @Override
    public Set<String> getKeys(String pattern) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                TreeSet<String> keyValue = new TreeSet<>();
                Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
                for (String key : clusterNodes.keySet()) {
                    JedisPool jedisPool = clusterNodes.get(key);
                    try (Jedis jedisConn = jedisPool.getResource()) {
                        keyValue.addAll(jedisConn.keys(pattern));
                    }
                }
                return keyValue;
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.keys(pattern);
                }
            } else if (REDIS_WORK_MODE_SHARDED.equals(clusterMode)) {
                try (ShardedJedis shardedJedis = ((ShardedJedisPool) redisClient).getResource()) {
                    return shardedJedis.hkeys(pattern);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.keys(pattern);
            }
        }
    }

    @Override
    public List<String> scan(String pattern) {
        List<String> keysList = new ArrayList<>();
        ScanParams paramas = new ScanParams();
        paramas.match(pattern);
        paramas.count(200);
        String cursor = "0";
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                Map<String, JedisPool> clusterNodes = jedisCluster.getClusterNodes();
                for (String key : clusterNodes.keySet()) {
                    String scanCursor = "0";
                    JedisPool jedisPool = clusterNodes.get(key);
                    do {
                        try (Jedis jedis = jedisPool.getResource()) {
                            ScanResult<String> clusterScanResult = jedis.scan(scanCursor, paramas);
                            scanCursor = clusterScanResult.getCursor();
                            if (null != clusterScanResult.getResult() && !clusterScanResult.getResult().isEmpty()) {
                                keysList.addAll(clusterScanResult.getResult());
                            }
                        }
                    } while (!scanCursor.equals("0"));
                }
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                do {
                    try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                        ScanResult<String> scanResult = jedis.scan(cursor, paramas);
                        cursor = scanResult.getCursor();
                        if (null != scanResult.getResult() && !scanResult.getResult().isEmpty()) {
                            keysList.addAll(scanResult.getResult());
                        }
                    }
                } while (!cursor.equals("0"));
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            do {
                try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                    ScanResult<String> scanResult = jedis.scan(cursor, paramas);
                    cursor = scanResult.getCursor();
                    if (null != scanResult.getResult() && !scanResult.getResult().isEmpty()) {
                        keysList.addAll(scanResult.getResult());
                    }
                }
            } while (!cursor.equals("0"));
        }
        return keysList.stream().distinct().collect(Collectors.toList());
    }

    @Override
    public long publish(String channel, String message) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                return jedisCluster.publish(channel, message);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    return jedis.publish(channel, message);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                return jedis.publish(channel, message);
            }
        }
    }

    @Override
    public void psubscribe(JedisPubSub subscribeListener, String channel) {
        if (clusterFlag) {
            if (REDIS_WORK_MODE_CLUSER.equals(clusterMode)) {
                JedisCluster jedisCluster = (JedisCluster) redisClient;
                jedisCluster.psubscribe(subscribeListener, channel);
            } else if (REDIS_WORK_MODE_SENTINEL.equals(clusterMode)) {
                try (Jedis jedis = ((JedisSentinelPool) redisClient).getResource()) {
                    jedis.psubscribe(subscribeListener, channel);
                }
            } else {
                rglog.error("不支持的集群工作模式:{}", clusterMode);
                throw new BizException("不支持的集群工作模式");
            }
        } else {
            try (Jedis jedis = ((JedisPool) redisClient).getResource()) {
                jedis.psubscribe(subscribeListener, channel);
            }
        }
    }

    /**
     * 初始化集群哨兵工作模式Redis连接池
     */
    private JedisSentinelPool initJesitSentinelPool(RedisConfig redisConfig) {
        JedisSentinelPool jedisSentinelPool;
        JedisPoolConfig jedisPoolConfig = initJedisPoolConfig(redisConfig);
        Set<String> sentinelSet = Arrays.stream(redisConfig.getClusterNodes().split(",")).collect(Collectors.toSet());
        if (StringUtil.isEmptyOrNull(redisConfig.getPassword())) {
            jedisSentinelPool = new JedisSentinelPool(redisConfig.getMasterName(), sentinelSet, jedisPoolConfig, redisConfig.getTimeout(), null, redisConfig.getDataBaseNo());
        } else {
            jedisSentinelPool = new JedisSentinelPool(redisConfig.getMasterName(), sentinelSet, jedisPoolConfig, redisConfig.getTimeout(), redisConfig.getPassword(), redisConfig.getDataBaseNo());
        }
        return jedisSentinelPool;
    }

    private ShardedJedisPool initShardedJedisPoll(RedisConfig redisConfig) {
        ShardedJedisPool shardedJedisPool;
        GenericObjectPoolConfig genericObjectPoolConfig = initGenericObjectPoolConfig(redisConfig);
        List<JedisShardInfo> shardInfos = new ArrayList<>();
        String[] addressArr = redisConfig.getClusterNodes().trim().split(",");
        for (String s : addressArr) {
            String[] ipAndPort = s.split(":");
            shardInfos.add(new JedisShardInfo(ipAndPort[0], Integer.parseInt(ipAndPort[1])));
        }
        shardedJedisPool = new ShardedJedisPool(genericObjectPoolConfig, shardInfos);
        return shardedJedisPool;
    }

    /**
     * 初始化集群环境Redis的连接池
     */
    private JedisCluster initJedisCluster(RedisConfig redisConfig) {
        JedisCluster jedisCluster;
        JedisPoolConfig jedisPoolConfig = initJedisPoolConfig(redisConfig);
        if (StringUtil.isEmptyOrNull(redisConfig.getPassword())) {
            jedisCluster = new JedisCluster(parseHostAndPort(redisConfig.getClusterNodes()), jedisPoolConfig);
        } else {
            jedisCluster = new JedisCluster(parseHostAndPort(redisConfig.getClusterNodes()), redisConfig.getTimeout(), redisConfig.getTimeout(), redisConfig.getMaxAttempts(), redisConfig.getPassword(), jedisPoolConfig);
        }
        return jedisCluster;
    }

    /**
     * 初始化单机环境Redis的连接池
     */
    private JedisPool initJedisPool(RedisConfig redisConfig) {
        JedisPool redisPool;
        JedisPoolConfig jedisPoolConfig = initJedisPoolConfig(redisConfig);
        if (StringUtil.isEmptyOrNull(redisConfig.getPassword())) {
            redisPool = new JedisPool(jedisPoolConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getTimeout(), null, redisConfig.getDataBaseNo());
        } else {
            redisPool = new JedisPool(jedisPoolConfig, redisConfig.getHost(), redisConfig.getPort(), redisConfig.getTimeout(), redisConfig.getPassword(), redisConfig.getDataBaseNo());
        }
        return redisPool;
    }

    /**
     * 解析集群地址
     *
     * @param clusterNodes 集群节点
     * @return 集群地址解析结果
     */
    private Set<HostAndPort> parseHostAndPort(String clusterNodes) {
        String[] addressArr = clusterNodes.trim().split(",");
        Set<HostAndPort> haps = new LinkedHashSet<>();
        for (String addressStr : addressArr) {
            String[] ipAndPort = addressStr.trim().split(":");
            HostAndPort hap = new HostAndPort(ipAndPort[0].trim(), Integer.parseInt(ipAndPort[1].trim()));
            haps.add(hap);
        }
        return haps;
    }

    /**
     * 初始化Redis连接池配置
     *
     * @param redisConfig Redis配置文件
     * @return Redis连接池配置
     */
    private JedisPoolConfig initJedisPoolConfig(RedisConfig redisConfig) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(redisConfig.getMaxTotal());
        jedisPoolConfig.setMaxIdle(redisConfig.getMaxIdle());
        jedisPoolConfig.setMaxWaitMillis(redisConfig.getMaxWaitMillis());
        jedisPoolConfig.setTestOnBorrow(redisConfig.isTestOnBorrow());
        jedisPoolConfig.setTestOnReturn(redisConfig.isTestOnReturn());
        jedisPoolConfig.setTestWhileIdle(redisConfig.isTestWhileIdle());
        jedisPoolConfig.setMinIdle(1);
        return jedisPoolConfig;
    }

    /**
     * 初始化Redis连接池配置
     *
     * @param redisConfig Redis配置文件
     * @return Redis连接池配置
     */
    private GenericObjectPoolConfig initGenericObjectPoolConfig(RedisConfig redisConfig) {
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
        genericObjectPoolConfig.setMaxTotal(redisConfig.getMaxTotal());
        genericObjectPoolConfig.setMaxIdle(redisConfig.getMaxIdle());
        genericObjectPoolConfig.setMaxWaitMillis(redisConfig.getMaxWaitMillis());
        genericObjectPoolConfig.setTestOnBorrow(redisConfig.isTestOnBorrow());
        genericObjectPoolConfig.setTestOnReturn(redisConfig.isTestOnReturn());
        genericObjectPoolConfig.setTestWhileIdle(redisConfig.isTestWhileIdle());
        genericObjectPoolConfig.setMinIdle(1);
        return genericObjectPoolConfig;
    }
}
